﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using System.Web;
using Slam.FieldTypes;
using SlamCms.SharePoint.Core;
using System.Linq;
using SlamCms.Configuration;
using SlamCms.Data;

namespace SlamCms.SharePoint.FieldTypes
{
	public partial class SlamIDLinkFieldControl : SlamFieldControl
	{
		protected Label lblSlamID;
		protected HyperLink lnkView;

		public override object Value
		{
			get
			{
				this.EnsureChildControls();

				return this.FieldValue;
			}
		}
		

		protected override string DefaultTemplateName
		{
			get
			{
				return "SlamCMSSlamIDLinkFieldControl";
			}
		}

		public override string DisplayTemplateName
		{
			get
			{
				return "SlamCMSSlamIDLinkFieldControl";
			}
			set
			{
				base.DisplayTemplateName = value;
			}
		}


		protected override void CreateChildControls()
		{
			if (Field == null)
				return;

			base.CreateChildControls();

			if (ControlMode == SPControlMode.New || ListItem["SLAM ID"] == null)
				return;

			if (!base.IsFieldValueCached)
			{
				string slamId = ListItem["SLAM ID"].ToString();
				lblSlamID = (Label)TemplateContainer.FindControl("lblSlamID");
				lblSlamID.Text = slamId.Replace("-p", "");

				var zone = SlamCmsContext.Data.Zones.Where(z => z.Sites.Any(s => s.SiteId == Web.ID)).FirstOrDefault();

				string url = "";
				if (zone != null && !String.IsNullOrEmpty(slamId))
				{
					var item = SlamCmsContext.Data.CreateQuery().FilterStatus(FilterStatus.None).FilterZone(zone.ZoneId).Filter("ContentItem.ContentItemID = '{0}'", slamId).Cache(QueryCacheBehavior.NoCache).Get().FirstOrDefault();
					if (item != null)
						url = ConfigurationManager.Environment.DefaultApplication.FrontEndUrl.TrimEnd('/') + "/" + item.FrontEndUrl.TrimStart('/');
				}

				lnkView = (HyperLink)TemplateContainer.FindControl("lnkView");
				lnkView.NavigateUrl = url;
			}
		}

		//protected override void CreateChildControls()
		//{
		//    if (Field == null)
		//        return;

		//    base.CreateChildControls();

		//    if (ControlMode == SPControlMode.Display)
		//        return;

		//    if (!base.IsFieldValueCached)
		//    {
		//        lblFieldValue = (Label)TemplateContainer.FindControl("lblFieldValue");
		//        lblFieldValue.Text = this.FixedValue;
		//    }
		//}

		private string fieldValue;

		public string FieldValue
		{
			get { return fieldValue; }
			set { fieldValue = value; }
		}


		protected override void OnLoad(EventArgs e)
		{
			if (ControlMode == SPControlMode.New || ListItem["SLAM ID"] == null)
			{
				this.Parent.Parent.Visible = true;
				base.OnLoad(e);
				this.Parent.Parent.Visible = false;
			}
		}

		//protected override void OnInit(EventArgs e)
		//{
		//    base.OnInit(e);

		//    if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
		//    {
		//        SPListItem item = SPContext.Current.Item as SPListItem;

		//        string contentType = HttpContext.Current.Request.QueryString["ContentTypeId"];
		//        string isDialog = HttpContext.Current.Request.QueryString["IsDlg"];

		//        string url = String.Concat(item.Web.Url, "/", item.ParentList.Forms[PAGETYPE.PAGE_EDITFORM].Url, "?id=", item.ID.ToString(), "&ContentTypeId=", contentType, "&IsDlg=", isDialog);

		//        HttpContext.Current.Response.Redirect(url);
		//    }
		//}

	}

}
